<?php

namespace Xin\ThinkPHP\Util;

use think\db\exception\PDOException;
use think\facade\Db;

class DbUtil
{

    /**
     * @param callable $callback
     * @return void
     * @throws PDOException
     */
    public static function call(callable $callback)
    {
        try {
            return $callback();
        } catch (PDOException $e) {
            $data = $e->getData();
            if (isset($data['PDO Error Info']) && $pdoErrorInfo = $data['PDO Error Info']) {
                // Numeric value out of range: 0 Out of range value for col
                if ($pdoErrorInfo['SQLSTATE'] == 22003) {
                    return;
                }
            }

            throw $e;
        }
    }

    /**
     * @return void
     * @noinspection ForgottenDebugOutputInspection
     */
    public static function dump()
    {
        Db::listen(static function ($sql) {
            dump($sql);
        });
    }

}
